from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from typing import List
from db import get_db
from models.content import Content
from schemas.content import ContentCreate, ContentOut

router = APIRouter()


@router.get("/", response_model=List[ContentOut])
def list_content(content_type: str = None, db: Session = Depends(get_db)):
    q = db.query(Content)
    if content_type:
        q = q.filter(Content.type == content_type)
    return q.order_by(Content.id.desc()).limit(200).all()


@router.post("/", response_model=ContentOut)
def create_content(data: ContentCreate, db: Session = Depends(get_db)):
    item = Content(**data.dict())
    db.add(item)
    db.commit()
    db.refresh(item)
    return item








